import { customRef } from "vue";

export function debounceRef(value, delay = 1000) {
  let timer;
  return customRef((track, trigger) => {
    // 获得 track, trigger 函数
    return {
      get() {
        // 依赖收集 track()
        track();
        return value;
      },
      set(val) {
        if(timer != null){
            clearTimeout(timer)
            timer = null
        }
        timer = setTimeout(() => {
          value = val;
          // 派发更新 trigger()
          trigger();
        }, delay);
      },
    };
  });
}
export function debounce(func, wait) {  
    let timeout;  
    return function() {  
      const context = this;  
      const args = arguments;  
      clearTimeout(timeout);  
      timeout = setTimeout(() => func.apply(context, args), wait);  
    };  
  }
